package com.example.traffic_learning;

import global.global;
import database.DatabaseHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class MainMenu extends Activity{
	
	private ImageButton itemLawLearning;
	private ImageButton itemStudy;
	private ImageButton itemTest;
	private ImageButton itemTrafficSignSystem;
	private ImageButton itemTrafficReconize;
	private ImageButton itemAbout;
	private Intent nextActivity;
	private AlertDialog alertDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu);
		if(global.isExit){
			global.isExit = false;
			finish();
		}
		nextActivity = new Intent(this, MainActivity.class);
		//Create database first time run
		DatabaseHelper myDbHelper = DatabaseHelper.getInstance(this);
       	myDbHelper.createDatabase();
		itemLawLearning = (ImageButton)findViewById(R.id.item_law_learning);
		itemLawLearning.setOnClickListener(new View.OnClickListener() {	
			@Override
			public void onClick(View v) {
				nextActivity.putExtra("possition", 1);
				startActivity(nextActivity);
			}
		});
		
		itemStudy = (ImageButton)findViewById(R.id.item_study);
		itemStudy.setOnClickListener(new View.OnClickListener() {		
			@Override
			public void onClick(View v) {
				nextActivity.putExtra("possition", 2);
				startActivity(nextActivity);
			}
		});
		
		itemTest = (ImageButton)findViewById(R.id.item_test);
		itemTest.setOnClickListener(new View.OnClickListener() {		
			@Override
			public void onClick(View v) {
				nextActivity.putExtra("possition", 3);
				startActivity(nextActivity);
			}
		});
		
		itemTrafficSignSystem = (ImageButton)findViewById(R.id.item_sign_system);
		itemTrafficSignSystem.setOnClickListener(new View.OnClickListener() {		
			@Override
			public void onClick(View v) {
				nextActivity.putExtra("possition", 4);
				startActivity(nextActivity);
			}
		});
		
		itemTrafficReconize = (ImageButton)findViewById(R.id.item_reconize);
		itemTrafficReconize.setOnClickListener(new View.OnClickListener() {		
			@Override
			public void onClick(View v) {
				nextActivity.putExtra("possition", 5);
				startActivity(nextActivity);
			}
		});
		
		itemAbout = (ImageButton)findViewById(R.id.item_about);
		itemAbout.setOnClickListener(new View.OnClickListener() {		
			@Override
			public void onClick(View v) {
				nextActivity.putExtra("possition", 6);
				startActivity(nextActivity);
			}
		});
		getActionBar().setTitle("Học luật giao thông đường bộ");
	}
	@Override
	public void onBackPressed() {
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
		
		alertDialogBuilder.setMessage("Bạn có muốn thoát chương trình không?");
		alertDialogBuilder.setCancelable(false).setPositiveButton("Có", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				finish();
			}
		}).setNegativeButton("Không", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		if(alertDialog == null){
			alertDialog = alertDialogBuilder.create();
		}
		alertDialog.show();
	}
}
